package com.ctshk.sapp.order.bus.controller;

import javax.validation.Valid;

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.order.bus.dto.BusOrderReceiptInfoDTO;
import com.ctshk.rpc.order.bus.req.BusOrderReceiptReq;
import com.ctshk.rpc.order.bus.service.IBusOrderReceiptService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 巴士订单收据 前端控制器
 * </p>
 *
 * @author 彭柳霖
 * @since 2021-03-18
 */
@Api(value="订单中心-收据管理", tags="订单中心-收据管理")
@RestController
@RequestMapping("/bus-order-receipt")
public class BusOrderReceiptController extends BaseController {

    @DubboReference
    private IBusOrderReceiptService busOrderReceiptService;

    @ApiOperation(value = "火车票-订单中心-保存收据", notes = "火车票-订单中心-保存收据")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "火车票-订单中心-保存收据", operDesc = "火车票-订单中心-保存收据")
    @PostMapping("/addOrder")
    public Result saveTrainReceipt(@Valid @RequestBody BusOrderReceiptReq busOrderReceiptReq) {
        TokenUser currentUser = getCurrentUser(getRequest());
        return busOrderReceiptService.saveTrainReceipt(busOrderReceiptReq, currentUser);
    }

    @ApiOperation(value = "订单中心-查询收据详情", notes = "订单中心-查询收据详情")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "订单中心-查询收据详情", operDesc = "订单中心-查询收据详情")
    @GetMapping(value = "/queryOrderReceiptInfo/{orderId}")
    public Result<BusOrderReceiptInfoDTO> queryOrderReceiptInfo(@PathVariable Long orderId){
        return busOrderReceiptService.queryOrderReceiptInfo(orderId);
    }
    
    @ApiOperation("下载收据")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.OTHER, operTitle = "下载收据", operDesc = "下载收据")
    @GetMapping("downloadReceipt/{orderId}")
    public ResponseEntity<byte[]> downloadReceipt(@PathVariable Long orderId) {
    	byte[] body = "123".getBytes();
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attchement;filename=" + "123.pdf");
        HttpStatus statusCode = HttpStatus.OK;
        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
        return entity;
	}
    
    @ApiOperation("收据发送邮件")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.OTHER, operTitle = "收据发送邮件", operDesc = "收据发送邮件")
    @GetMapping("sendEmailReceipt/{orderId}")
    public Result<Void> sendEmailReceipt(@PathVariable Long orderId) {
		return busOrderReceiptService.sendEmailReceipt(orderId);
	}
}
